package LeetCode;

import java.util.Stack;

public class LC_173_BinarySearchTreeIterator {

    public static void main(String[] args) {

    }

    public class BSTIterator {
        Stack<TreeNode> stack = new Stack<>();

        public BSTIterator(TreeNode root) {
            while (root != null) {
                stack.push(root);
                root = root.left;
            }
        }

        public boolean hasNext() {
            return !stack.isEmpty();
        }

        public int next() {
            TreeNode minCurrent = stack.pop();
            if (minCurrent.right != null) {
                TreeNode rightNode = minCurrent.right;
                while (rightNode != null) {
                    stack.push(rightNode);
                    rightNode = rightNode.left;
                }
            }
            return minCurrent.val;
        }

    }
}